﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using DTO;
using System.Collections;

namespace BLL
{
    public class chucvubll
    {
        static int id = 0;
        QuanLyDiemDataContext DB = new QuanLyDiemDataContext();
        public IQueryable chonchucvu()
        {
            return DB.ChucVus.OrderByDescending(s => s.MaChucVu).AsQueryable();
        }
        public bool Insert(chucvuDTO chucvu)
        {
            try
            {
                ChucVu cv = new ChucVu();
                cv.MaChucVu = (id++).ToString();
                cv.TenChucVu = chucvu.TenChucVu;


                DB.ChucVus.InsertOnSubmit(cv);
                DB.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public string Update(chucvuDTO chucvu)
        {
            try
            {
                ChucVu cv = DB.ChucVus.Where(s => s.MaChucVu == chucvu.MaChucVu).FirstOrDefault();
                cv.MaChucVu = chucvu.MaChucVu;
                cv.TenChucVu = chucvu.TenChucVu;
                DB.SubmitChanges();
                return "Thanh cong";

            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        public string Delete()
        {
            try
            {
                DB.SubmitChanges();
                return "Thanh cong";

            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        public List<chucvuDTO> getChucVu()
        {
            //lay het du lieu tu bang khoi
            List<ChucVu> list = DB.ChucVus.ToList<ChucVu>();
            //tao moi 1 doi tuong KhoiDTO de chua du lieu cua bang khoi
            List<chucvuDTO> listdto = new List<chucvuDTO>();
            foreach (ChucVu nam in list)
            {
                chucvuDTO k = new chucvuDTO();
                k.MaChucVu = nam.MaChucVu;
                k.TenChucVu = nam.TenChucVu;
                listdto.Add(k);
            }
            return listdto;
        }

    }
}
